import route from '../../router'
const state = {
    activeRouter:[], // 当前已经打开的页面
    activeComponent:null,
}

const mutations = {
    SET_ACTIVE_ROUTER(state,obj){ // 新增菜单
        state.activeComponent = obj
        if(!state.activeRouter.find(n=>n.url===obj.url)){
            state.activeRouter.push(obj)
           route.push(obj.url)
        }
    },
    SwitchActiveComponent(state,name){ // 菜单切换
        state.activeComponent = state.activeRouter.find(n=>n.url===name)
        route.push(state.activeComponent ? state.activeComponent.url : '/')
    },
    DelActiveRouter(state,url){ // 删除菜单
        state.activeRouter = state.activeRouter.filter(n=>n.url!==url)
        if(state.activeRouter.length>0){
            state.activeComponent = state.activeRouter[state.activeRouter.length - 1]
        }else{
            state.activeComponent=null
        }
        let path = state.activeComponent ? state.activeComponent.url : '/'
        if(vm.$route.path!==path){
            route.push({path})
        }

    }
}

const actions = {
    setRouter({commit},r){
        const obj = {
            url:r.moduleUrl,
            key:r.moduleKey,
            id:r.id,
            title:r.moduleName
        }
        commit('SET_ACTIVE_ROUTER',obj)
    },

}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
